/*
 * Copyright (c) 2009-2011, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef ALKES_IVERTEX_BUFFER_H_INCLUDED_
#define ALKES_IVERTEX_BUFFER_H_INCLUDED_

#include "alkes/internal.h"
#include "alkes/core/TRefCount.h"
#include "alkes/core/Types.h"
#include "alkes/core/ClassID.h"

namespace alkes {

/// \addtogroup graphics
//@{

enum VertexType
{
    // TODO: check D3DVERTEXELEMENT9
    AL_VERTEX_XY        = 0x0001,    ///< 2D position
    AL_VERTEX_XYZ       = 0x0002,    ///< 3D position
    AL_VERTEX_NORMAL    = 0x0004,    ///< vertex normal
    AL_VERTEX_COLOR     = 0x0010,    ///< diffuse color
    AL_VERTEX_SPECULAR  = 0x0020,    ///< specular color
    AL_VERTEX_UV1       = 0x0100,    ///< texture coordinate1
    AL_VERTEX_UV2       = 0x0200,    ///< texture coordinate2
    AL_VERTEX_UV3       = 0x0400,    ///< texture coordinate3
    AL_VERTEX_UV4       = 0x0800,    ///< texture coordinate4
};

/** Vertex buffer interface.
*/
class AL_EXPORT IVertexBuffer
    : public TRefCount<IVertexBuffer>
{
    AL_DECLARE_ROOT_CLASS(IVertexBuffer);

public:

    /** Set 2D vertex array.
    @param [in] index
    @param [in] count
    @param [in] vertex
    @attention In many case, 2D vertex is set as 3D vertex.(z=0.0f)
    */
    void setVertex2D(int32_t index, int32_t count, const Vector2D* vertex);

    /** Set 3D vertex array.
    @param [in] index
    @param [in] count
    @param [in] vertex
    */
    void setVertex3D(int32_t index, int32_t count, const Vector3D* vertex);

    /** Set normal vertex array.
    @param [in] index
    @param [in] count
    @param [in] vertex
    */
    void setNormal(int32_t index, int32_t count, const Vector3D* vertex);

    /** Set diffuse color array.
    @param [in] index
    @param [in] count
    @param [in] color
    */
    void setDiffuse(int32_t index, int32_t count, const Color* color);

    /** Set specular color array.
    @param [in] index
    @param [in] count
    @param [in] color
    */
    void setSpecular(int32_t index, int32_t count, const Color* color);

    /** Set texture coordinate array.
    @param [in] index
    @param [in] count
    @param [in] type
    @param [in] texcoord
    */
    void setTexCoord(int32_t index, int32_t count, VertexType type, const Vector2D* texcoord);

    /** Set 2D vertex and diffuse color array.
    @param [in] index
    @param [in] count
    @param [in] vertex
    @param [in] color
    */
    void setVertex2DDiffuse(int32_t index, int32_t count, const Vector2D* vertex, const Color* color);

    /** Set 2D vertex and texture coordinate array.
    @param [in] index
    @param [in] count
    @param [in] vertex
    @param [in] texcoord
    */
    void setVertex2DTexCoord(int32_t index, int32_t count, const Vector2D* vertex, const Vector2D* texcoord);

    /**
    @retval true
    @retval false
    */
    bool isDirty() const;

    /** Get vertex count.
    @return Current vertex count.
    */
    virtual int32_t getVertexCount() = 0;

    /** Get vertex type.
    */
    uint32_t getVertexType() const;

protected:
    friend class TRefCount<IVertexBuffer>;

    enum IVertexBufferFlag
    {
        AL_VB_DIRTY = 0,
        AL_DERIVED_CLASS_USABLE = 8,
    };

    IVertexBuffer(uint32_t vertex_type);
    virtual ~IVertexBuffer() = 0;
    virtual void destroy() = 0;

    void setDirty(bool is_dirty);

    virtual bool onSetVertex2D(int32_t index, int32_t count, const Vector2D* vertex) = 0;
    virtual bool onSetVertex3D(int32_t index, int32_t count, const Vector3D* vertex) = 0;
    virtual bool onSetNormal(int32_t index, int32_t count, const Vector3D* vertex) = 0;
    virtual bool onSetDiffuse(int32_t index, int32_t count, const Color* color) = 0;
    virtual bool onSetSpecular(int32_t index, int32_t count, const Color* color) = 0;
    virtual bool onSetTexCoord(int32_t index, int32_t count, VertexType type, const Vector2D* texcoord) = 0;
    virtual bool onSetVertex2DDiffuse(int32_t index, int32_t count, const Vector2D* vertex, const Color* color);
    virtual bool onSetVertex2DTexCoord(int32_t index, int32_t count, const Vector2D* vertex, const Vector2D* texcoord);

private:
    uint32_t vertex_type_;
};

//@}

}

#endif
